package org.csu.jpetstore_manage_system.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.jpetstore_manage_system.domain.Order;
import org.csu.jpetstore_manage_system.domain.OrderStatus;
import org.csu.jpetstore_manage_system.persistence.ItemMapper;
import org.csu.jpetstore_manage_system.persistence.LineItemMapper;
import org.csu.jpetstore_manage_system.persistence.OrderMapper;
import org.csu.jpetstore_manage_system.persistence.OrderStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("OrderService")
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private LineItemMapper lineItemMapper;
    @Autowired
    private OrderStatusMapper orderStatusMapper;

    public List<Order> getAllOrderList(){
        List<Order> orderList = orderMapper.selectList(new UpdateWrapper<>());
        for(Order order: orderList){
            OrderStatus orderStatus = orderStatusMapper.selectById(order.getOrderId());
            order.setStatus(orderStatus.getStatus());
        }
        return orderList;
    };

    public List<Order> getOrderListByUsername(String username){
        List<Order> orderList = orderMapper.selectList(new UpdateWrapper<Order>().eq("userid", username));
        for(Order order: orderList){
            OrderStatus orderStatus = orderStatusMapper.selectById(order.getOrderId());
            order.setStatus(orderStatus.getStatus());
        }
        return orderList;
    };

    public boolean updateOrder(int orderId, String username, Date orderDate, String shipAddress1, BigDecimal totalPrice){
        Order order = orderMapper.selectById(orderId);
        order.setUsername(username);
        order.setOrderDate(orderDate);
        order.setShipAddress1(shipAddress1);
        order.setTotalPrice(totalPrice);

        int count = orderMapper.updateById(order);
        return count != 0;
    }


    public Order getOrderByOrderId(int orderId){
        Order order = orderMapper.selectById(orderId);
        return order;
    };

    public boolean updateOrderStatus(Order order) {
        int count = orderMapper.updateById(order);
        UpdateWrapper<OrderStatus> orderStatusUpdateWrapper = new UpdateWrapper<>();
        orderStatusUpdateWrapper.set("status", order.getStatus()).eq("orderid", order.getOrderId());
        orderStatusMapper.update(null, orderStatusUpdateWrapper);
        return count != 0;
    }

    public boolean deleteOrder(Order order){
        int orderId = order.getOrderId();
        int count = orderMapper.deleteById(orderId);
        lineItemMapper.deleteById(orderId);
        orderStatusMapper.deleteById(orderId);
        return count != 0;
    };

    public boolean deleteOrderById(int orderId){
        int count = orderMapper.deleteById(orderId);
        lineItemMapper.deleteById(orderId);
        orderStatusMapper.deleteById(orderId);
        return count != 0;
    };
}
